package com.htc.model.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;

import com.htc.common.ActionEvent;
import com.htc.constants.Constants;
import com.htc.controller.NewsModelController;
import com.htc.dlc.MainActivity;
import com.htc.dlc.fragment.FragmentHome;
import com.htc.dlc.fragment.FragmentNews;
import com.htc.model.dto.NewsModuleDTO;
import com.htc.utils.CommonObjects;

public class NewsModel {
	private ActionEvent actionEvent;
	public static NewsModel instance;

	public static NewsModel getInstance() {
		if (instance == null)
			instance = new NewsModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {
		this.actionEvent = e;
		switch (e.action) {
		case Constants.GET_NEWS_MODEL:
			ArrayList<NewsModuleDTO> home = getAllNews((Object) e.sender);
			e.viewData = home;
			NewsModelController.getInstance().handleModelViewEvent(e);
			break;

		case Constants.GET_NEWS_MODEL1:
			ArrayList<NewsModuleDTO> news = getAllNews1((Object) e.sender);
			e.viewData = news;
			NewsModelController.getInstance().handleModelViewEvent(e);
			break;

		default:
			break;
		}
	}

	public void getAllEvent1(Context contex) {
		ArrayList<NewsModuleDTO> arr = getAllNews1(contex);
		for (Object o : arr) {
			NewsModuleDTO dto = (NewsModuleDTO) o;
			ActionEvent e = new ActionEvent(actionEvent.sender,
					Constants.GET_NEWS_MODEL1, dto, null);
			NewsModelController.getInstance().handleModelViewEvent(e);
		}
	}

	public ArrayList<NewsModuleDTO> getAllNews(Object ob) {

		ArrayList<NewsModuleDTO> arrNews = new ArrayList<NewsModuleDTO>();
		String sql = "SELECT * from news_module";
		FragmentHome home = (FragmentHome) ob;
		Context ctx = home.getActivity().getBaseContext();
		DBHelper db = DBHelper.getInstance(ctx);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			NewsModuleDTO dto = new NewsModuleDTO();
			dto.setNewsModuleId(c.getString(c.getColumnIndex("news_module_id")));
			dto.setNewsModuleTitle(c.getString(c
					.getColumnIndex("news_module_title")));
			dto.setNewsModuleDescription(c.getString(c
					.getColumnIndex("news_module_description")));
			dto.setNewsModuleImage(c.getString(c
					.getColumnIndex("news_module_image")));
			// dto.setNewsAddedDate(c.getString(c.getColumnIndex("added_date")));
			dto.setNewsPriority(c.getString(c.getColumnIndex("priority")));
			dto.setNewsModuleTitleAr(c.getString(c
					.getColumnIndex("news_module_title_ar")));
			dto.setNewsModuleDescriptionAr(c.getString(c
					.getColumnIndex("news_module_description_ar")));
			arrNews.add(dto);

		}
		c.close();
		db.close();
		return arrNews;
	}

	public ArrayList<NewsModuleDTO> getAllNews1(Object ob) {

		ArrayList<NewsModuleDTO> arrNews = new ArrayList<NewsModuleDTO>();
		String sql = "SELECT * from news_module";
		FragmentNews home = (FragmentNews) ob;
		Context ctx = home.getActivity().getBaseContext();
		DBHelper db = DBHelper.getInstance(ctx);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			NewsModuleDTO dto = new NewsModuleDTO();
			dto.setNewsModuleId(c.getString(c.getColumnIndex("news_module_id")));
			dto.setNewsModuleTitle(c.getString(c
					.getColumnIndex("news_module_title")));
			dto.setNewsModuleDescription(c.getString(c
					.getColumnIndex("news_module_description")));
			dto.setNewsModuleImage(c.getString(c
					.getColumnIndex("news_module_image")));
			// dto.setNewsAddedDate(c.getString(c.getColumnIndex("added_date")));
			dto.setNewsPriority(c.getString(c.getColumnIndex("priority")));
			dto.setNewsModuleTitleAr(c.getString(c
					.getColumnIndex("news_module_title_ar")));
			dto.setNewsModuleDescriptionAr(c.getString(c
					.getColumnIndex("news_module_description_ar")));
			arrNews.add(dto);

		}
		c.close();
		db.close();
		return arrNews;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT news_module_id FROM " + Constants.TBL_NEWS_MODULE
				+ " where news_module_id='" + id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertNews(NewsModuleDTO news, Context context) {
		boolean flag = false;
		if (!checkDB(context, news.getNewsModuleId())) {
			String DATABASE_TABLE = Constants.TBL_NEWS_MODULE;
			ContentValues values = new ContentValues();
			values.put("news_module_id", news.getNewsModuleId());
			values.put("news_module_title", news.getNewsModuleTitle());
			values.put("news_module_description",
					news.getNewsModuleDescription());
			values.put("news_module_image", news.getNewsModuleImage());
			values.put("added_date", news.getNewsAddedDate());
			values.put("priority", news.getNewsPriority());
			values.put("news_module_title_ar", news.getNewsModuleTitleAr());
			values.put("news_module_description_ar",
					news.getNewsModuleDescriptionAr());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateNews(NewsModuleDTO news, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_NEWS_MODULE;
		String where = "news_module_id='" + news.getNewsModuleId() + "'";
		ContentValues values = new ContentValues();

		values.put("news_module_title", news.getNewsModuleTitle());
		values.put("news_module_description", news.getNewsModuleDescription());
		values.put("news_module_image", news.getNewsModuleImage());
		values.put("added_date", news.getNewsAddedDate());
		values.put("priority", news.getNewsPriority());
		values.put("news_module_title_ar", news.getNewsModuleTitleAr());
		values.put("news_module_description_ar",
				news.getNewsModuleDescriptionAr());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteNews(NewsModuleDTO news, Context context) {
		String sql = "news_module_id='" + news.getNewsModuleId() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_NEWS_MODULE;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
